package com.hm.controller;

import com.hm.pojo.*;
import com.hm.serivce.HmAddressService;
import com.hm.serivce.HmCartService;
import com.hm.serivce.HmOrdersService;
import com.hm.utils.HmResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
@Api(value = "订单管理接口",tags = "订单管理接口")
public class HmOrdersController {

    @Autowired
    private HmOrdersService hmOrdersService;

    @Autowired
    private HmCartService hmCartService;

    @Autowired
    private HmAddressService hmAddressService;

    /**
     * 订单列表页面
     * @return
     */
    @RequestMapping(value = "/ShowOrdersList",method = RequestMethod.GET)
    @ApiOperation(value = "订单列表页面")
    public List<HmOrders> ShowOrdersList(){
       List<HmOrders> list =  hmOrdersService.ShowOrdersList();
       return list;
    }

    /**
     * 根据订单id查找订单详情
     * @param arr
     * @return
     */
    @RequestMapping(value = "/GetOrdersById",method = RequestMethod.GET)
    @ApiOperation(value = "根据订单id查找订单详情")
    public HmResult GetOrdersById(String[] arr){
        HmAddress hmAddress = hmAddressService.ShowDefaultAddress();
        List<HmOrders> list = new ArrayList<>();
        for (String oid : arr) {
            HmOrders hmOrders = hmOrdersService.GetOrdersById(oid);
            list.add(hmOrders);
        }
        QueryVo1 queryVo1 = new QueryVo1();
        queryVo1.setHmOrders(list);
        queryVo1.setHmAddress(hmAddress);
        return HmResult.ok(queryVo1);
    }

    /**
     * 删除订单信息
     * @param oid
     */
    @RequestMapping(value = "/DeteleOrdersById",method = RequestMethod.GET)
    @ApiOperation(value = "删除订单信息")
    public HmResult DeteleOrdersById(String oid) {
        hmOrdersService.DeteleOrdersById(oid);
        return HmResult.ok("删除订单成功!");
    }

    /**
     * 添加订单信息
     * @param arr
     */
    @RequestMapping(value = "/SaveOrders",method = RequestMethod.POST)
    @ApiOperation(value = "提交订单信息")
    public HmResult SaveOrders(String[] arr){

        for (String cartid : arr) {
            HmCart hmCart = hmCartService.ConfirmOrdes(cartid);
            HmOrders hmOrders = new HmOrders();
            hmOrders.setOid(cartid);
            hmOrders.setTitle(hmCart.getTitle());
            hmOrders.setImage(hmCart.getPic_path());
            hmOrders.setNum(hmCart.getNum());
            hmOrders.setNorm(hmCart.getNorm());
            hmOrders.setPrice(hmCart.getPrice());
            hmOrdersService.SaveOrders(hmOrders);
        }
        return HmResult.ok("提交订单成功!");
    }
}
